<?php
namespace app\admin\controller;
use think\Controller;
use app\index\model\Order as OrderModel;
use app\admin\model\Goods;

class Order extends Controller
{
	public function orderlist()
	{
		$orderModel = new OrderModel();
		$where = [
			'status' => ['neq',0],
		];
		$orders = $orderModel->where($where)->order('id','desc')->paginate(4);
		return $this->fetch('',[
			'orders' => $orders,
		]);
	}

	// 订单详情
	public function order_detail()
	{
		$orderModel = new OrderModel();
		$id = input('get.id');
		$order_detail = $orderModel->get($id);
		// dump($order_detail);exit;
		return $this->fetch('',['order_detail'=>$order_detail]);
	}

	// 发货，更改订单状态
	public function delivery($order_no)
	{
		$orderModel = new OrderModel();
		$goodsModel = new Goods();

		// 根据订单号查产品信息
		$orders = $orderModel->where('order_no',$order_no)->select();
		foreach ($orders as $goods) {
			$num = $goods->num;
			$gid = $goods->gid;
			$stock = $goodsModel->where('id',$gid)->column('stock')[0];
			$final = $stock-$num;
			$goodsModel->save(['stock'=>$final],['id'=>$gid]);
		}
		// 更改订单状态
		$res = $orderModel->save(['status'=>3],['order_no'=>$order_no]);
		if ($res) {
			$this->result($_SERVER['HTTP_REFERER'], 1, 'success');
		} else {
			$this->result($_SERVER['HTTP_REFERER'], 0, '更新失败');
		}		
	}
}